var dis = require('../../utils/distance.js')
var oth = require('../../utils/other.js')
const app = getApp();
Page({
  data: {
    current: 'tab1',
    tab1: true,
    venues:[]
  },
  onLoad: function (options) {
    this.getVenue(0)
  },  

  handleChange({ detail }) {
    var index = detail.key
    this.setData({
      current: detail.key
    });
    if (index == 'tab1') {
      this.setData({
        tab1: true,
        tab2: false,
        tab3: false
      })
      this.getVenue(0)
    }
    else if (index == 'tab2') {
      this.setData({
        tab1: false,
        tab2: true,
        tab3: false
      })
      this.getVenue(1)
    }
    else if (index == 'tab3') {
      this.setData({
        tab1: false,
        tab2: false,
        tab3: true
      })
      this.getVenue(2)
    }
  },

  goMap(e)
  {
    wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: parseFloat(e.currentTarget.dataset.lat),//要去的纬度-地址
          longitude: parseFloat(e.currentTarget.dataset.lng),//要去的经度-地址
          name: e.currentTarget.dataset.address,
          address: e.currentTarget.dataset.address
        })
      }
    })   
  },
  goStoreDetail(e)
  {
    console.log(e)
    wx.navigateTo({
      url: '../store_detail/store_detail?id='+e.currentTarget.dataset.id,
    })
  },
  getVenue(type)
  {
    var that = this
    wx.request({
      url: app.url + 'weiapp/api/venues',
      data: {
        type:type
      },
      success: function (res) {
        console.log(res.data)
        console.log(app.globalData.location)
        for (var index in res.data) {
          var distance = dis.getDistance(app.globalData.location['latitude'], app.globalData.location['longitude'], res.data[index]['lat'], res.data[index]['lng'])
          res.data[index]['distance'] = distance
          res.data[index]['juli'] = "距离约" + parseInt(distance / 1000) + "Km";

          //   this.getDistance(1,2,3,4);
        }
        console.log(res.data)
        let arr = oth.sort(res.data)
        that.setData({
          venues: arr
        })
      }
    })    
  }
});